package com.zgc.controller;


import com.zgc.common.R;
import com.zgc.dto.CountSaleDto;
import com.zgc.service.OrderService;
import com.zgc.util.MailSenderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/countS")
public class CountSaleContoller {
    @Autowired
    private OrderService orderService;
    @GetMapping("/list")
    public R findList(){
        List<CountSaleDto> cc=orderService.countAmonutByWeek();
        List<String> l1=new ArrayList<>();//存放时间的集合
        List<Double> l2=new ArrayList<>();//存放金额的集合
           for (int i=0;i<cc.size();i++){
            CountSaleDto countSaleDto=cc.get(i);
            l1.add(countSaleDto.getSj());
            l2.add(countSaleDto.getSl());
        }
        List list_c=new ArrayList<>();
           list_c.add(l1);
           list_c.add(l2);
           return R.success(list_c);
    }
    //每月按周的统计信息
    @GetMapping("/month_list")
    public R findMonth_list(){
        List<CountSaleDto> cc=orderService.countAmonutByMonth();
        List<String> l1=new ArrayList<>();//存放时间的集合
        List<Double> l2=new ArrayList<>();//存放金额的集合
        for (int i=0;i<cc.size();i++){
            CountSaleDto countSaleDto=cc.get(i);
            l1.add(countSaleDto.getSj());
            l2.add(countSaleDto.getSl());
        }
        List list_c=new ArrayList<>();
        list_c.add(l1);
        list_c.add(l2);
        return R.success(list_c);
    }
    @GetMapping("/dishCategory")
    public R dishCategory(){
        List<Map> list= orderService.countAmonutByDish();
        return R.success(list);
    }
    //封装，传入一个查询结果的集合，返回一个大集合，用于页面展示
    private List<List> getData(List<CountSaleDto> list){
//时间数据集合
        List<String> list_sj=new ArrayList<>();
//数量数据集合
        List<Double> list_sl=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            CountSaleDto cdto= list.get(i);
            list_sj.add( cdto.getSj());
            list_sl.add(cdto.getSl());
        }
        List<List> list_c=new ArrayList<>();
        list_c.add(list_sj);
        list_c.add(list_sl);
        return list_c;
    }
    //邮箱发送
    @Autowired
    private MailSenderUtil util;
    @GetMapping("/sendMail")
    public R<String> sendMail(String email){
        log.info("sendMail==="+email);
        if("".equals(email)||null==email){
            return R.success("邮箱不能为空");
        }
        String toUser = email;
        String title="外卖系统菜品分类统计分析结果";
        List<Map> list= orderService.countAmonutByDish();
        log.info("list==="+list);
        StringBuffer sb=new StringBuffer("菜品");
        for (int i = 0; i <list.size() ; i++) {
            Map map= list.get(i);
            String n= (String) map.get("name");
            sb.append(n+"的销售额是：");
            BigDecimal bigDecimal = (BigDecimal) map.get("value");
            sb.append(bigDecimal+",");
        }
        String body="您好，"+sb+"请注意查收";
        try {
            util.sendMail(toUser,title,body);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
        return R.success("邮件已发送");
    }
}


